home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / editwnd.cpp < prev    next >
C/C++ Source or Header  |  2001-10-08  |  7KB  |  286 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. //NONPORTABLE
  30.  
  31. #include "editwnd.h"
  32. #include "canvas.h"
  33. #include "notifmsg.h"
  34.  
  35. #include "../studio/assert.h"
  36. #include "std.h"
  37.  
  38. #define ID_EDITCHILD 12
  39.  
  40. enum { IDLETIMER=8 };
  41. #define IDLETIME 500
  42.  
  43. static LRESULT CALLBACK static_editWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  44.   EditWnd *editwnd = (EditWnd *)GetWindowLong(hWnd, GWL_USERDATA);
  45.   if (editwnd == NULL) return DefWindowProc(hWnd, uMsg, wParam, lParam);
  46.   return editwnd->editWndProc(hWnd, uMsg, wParam, lParam);
  47. }
  48.  
  49. EditWnd::EditWnd(char *buffer, int buflen) {
  50.   beforefirstresize=1;
  51.   editWnd = NULL;
  52.   prevWndProc = NULL;
  53.   setBuffer(buffer, buflen);
  54.   maxlen = 0;
  55.   retcode = EDITWND_RETURN_NOTHING;
  56.   modal = 0;
  57.   autoenter = 0;
  58.   autoselect = 0;
  59. //  bordered = 0;
  60. }
  61.  
  62. EditWnd::~EditWnd() {
  63.   killTimer(IDLETIMER);
  64.   if (editWnd != NULL) {
  65.     SetWindowLong(editWnd, GWL_USERDATA, (LONG)0);
  66.     SetWindowLong(editWnd, GWL_WNDPROC, (LONG)prevWndProc);
  67.     DestroyWindow(editWnd);
  68.   }
  69.   notifyParent(CHILD_RETURN_CODE, retcode);
  70. }
  71.  
  72. int EditWnd::onInit() {
  73.   EDITWND_PARENT::onInit();
  74.  
  75.   RECT r;
  76.   getClientRect(&r);
  77.  
  78.   editWnd = CreateWindow("EDIT", NULL,
  79.     WS_CHILD | ES_AUTOHSCROLL,
  80.     r.left, r.top, r.right - r.left, r.bottom - r.top,
  81.     gethWnd(), (HMENU)ID_EDITCHILD,
  82.     gethInstance(), NULL);
  83.   ASSERT(editWnd != NULL);
  84.  
  85.   if (outbuf != NULL) //FG
  86.     setBuffer(outbuf, maxlen);
  87.  
  88.   // stash a pointer to us
  89.   SetWindowLong(editWnd, GWL_USERDATA, (LONG)this);
  90.   // subclass the edit control
  91.   prevWndProc = (WNDPROC)SetWindowLong(editWnd, GWL_WNDPROC, (LONG)static_editWndProc);
  92.  
  93.   SendMessage(editWnd, WM_SETFONT, (WPARAM)GetStockObject(ANSI_VAR_FONT), FALSE);
  94.   return 1;
  95. }
  96.  
  97. int EditWnd::onPaint(Canvas *canvas) {
  98. //  if (!bordered) return EDITWND_PARENT::onPaint(canvas);
  99.  
  100.   PaintCanvas paintcanvas;
  101.   if (canvas == NULL) {
  102.     if (!paintcanvas.beginPaint(this)) return 0;
  103.     canvas = &paintcanvas;
  104.   }
  105.   EDITWND_PARENT::onPaint(canvas);
  106.  
  107.   RECT r;
  108.   getClientRect(&r);
  109.   canvas->drawRect(&r, TRUE, RGB(0, 0, 0));    //SKIN
  110.   
  111.   return 1;
  112. }
  113.  
  114. int EditWnd::onResize() {
  115.   EDITWND_PARENT::onResize();
  116.   RECT r;
  117.   getClientRect(&r);
  118.   if (1/*bordered*/) {
  119.     r.top++;
  120.     r.bottom--;
  121.     r.left++;
  122.     r.right--;
  123.   }
  124.   MoveWindow(editWnd, r.left, r.top, r.right-r.left, r.bottom-r.top, TRUE);
  125.  
  126.   if (beforefirstresize) { 
  127.     ShowWindow(editWnd, SW_NORMAL); beforefirstresize=0;
  128.     if (modal) {
  129.       SetFocus(editWnd);
  130.       if (getAutoSelect())
  131.         SendMessage(editWnd, EM_SETSEL, 0, -1);
  132.     }
  133.   }
  134.  
  135.   return TRUE;
  136. }
  137.  
  138. LRESULT EditWnd::wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  139.   switch (uMsg) {
  140. #if 0
  141.     case WM_CTLCOLOREDIT: {
  142.       HDC hdc = (HDC)wParam;
  143.       SetTextColor(hdc, RGB(0, 255, 0));        //SKIN
  144.     }
  145.     return (LRESULT)GetStockObject(WHITE_BRUSH);    //SKIN
  146. #endif
  147.  
  148.     case WM_COMMAND: {
  149.       switch (HIWORD(wParam)) {
  150.         case EN_CHANGE: {
  151.           if (maxlen > 0) {
  152.             *outbuf = 0;
  153.             int l = SendMessage(editWnd, WM_GETTEXT, (WPARAM)maxlen, (LPARAM)outbuf);
  154.             if (l <= 0) break;
  155.             outbuf[l] = 0;
  156.             onEditUpdate();
  157.           }
  158.         }
  159.         break;
  160.         case EN_KILLFOCUS:
  161.           onLoseFocus();
  162.         break;
  163.       }
  164.     }
  165.     break;
  166.   }
  167.   return EDITWND_PARENT::wndProc(hWnd, uMsg, wParam, lParam);
  168. }
  169.  
  170. void EditWnd::setBuffer(char *buffer, int len) {
  171.   if (buffer == NULL || len <= 0) return;
  172.   ASSERT(len > 0);
  173.   ASSERT(len < 0x7ffe);
  174.   ASSERT(STRLEN(buffer) < len);
  175.  
  176.   if (editWnd != NULL) {
  177.     SendMessage(editWnd, WM_SETTEXT, (WPARAM)0, (LPARAM)buffer);
  178.     SendMessage(editWnd, EM_LIMITTEXT, (WPARAM)len, (LPARAM)0);
  179.     if (getAutoSelect()) {
  180.       SetFocus(editWnd);
  181.       SendMessage(editWnd, EM_SETSEL, (WPARAM)0, (LPARAM)-1);
  182.     }
  183.   }
  184.  
  185.   outbuf = buffer;
  186.   maxlen = len;
  187. }
  188.  
  189. void EditWnd::getBuffer(char *buf, int _len) {
  190.   if (_len > maxlen) _len = maxlen;
  191.   MEMCPY(buf, outbuf, _len);
  192. }
  193.  
  194. void EditWnd::setModal(int _modal) {
  195.   modal = _modal;
  196. }
  197.  
  198. void setBorder(int border) {
  199. //  bordered = border;
  200. }
  201.  
  202. LRESULT EditWnd::editWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
  203.   switch (uMsg) {
  204.     case WM_CHAR:
  205.       if (wParam == VK_RETURN) {
  206.         if (onEnter()) return 0;
  207.         return 0;
  208.       } else if (wParam == VK_ESCAPE) {
  209.         if (onAbort()) return 0;
  210.       } 
  211.     break;
  212.   }
  213.   return CallWindowProc(prevWndProc, hWnd, uMsg, wParam, lParam);
  214. }
  215.  
  216. void EditWnd::timerCallback(int id) {
  217.   switch (id) {
  218.     case IDLETIMER:
  219.       killTimer(IDLETIMER);
  220.       onIdleEditUpdate();
  221.     break;
  222.     default:
  223.       EDITWND_PARENT::timerCallback(id);
  224.   }
  225. }
  226.  
  227. void EditWnd::onEditUpdate() {
  228.   killTimer(IDLETIMER);
  229.   setTimer(IDLETIMER, IDLETIME);
  230.   notifyParent(UMSG_EDIT_UPDATE);
  231. }
  232.  
  233. void EditWnd::onIdleEditUpdate() { }
  234.  
  235. int EditWnd::onEnter() {
  236.   notifyParent(CHILD_EDIT_DATA_MODIFIED);
  237.   notifyParent(CHILD_EDIT_ENTER_PRESSED);
  238.   if (modal) {
  239.     retcode = EDITWND_RETURN_OK;
  240.     delete this;
  241.     return 1;
  242.   }
  243.   return 0;
  244. }
  245.  
  246. int EditWnd::onAbort() {
  247.   if (modal) {
  248.     retcode = EDITWND_RETURN_CANCEL;
  249.     notifyParent(CHILD_EDIT_CANCEL_PRESSED);
  250.     delete this;
  251.     return 1;
  252.   }
  253.   return 0;
  254. }
  255.  
  256. int EditWnd::onLoseFocus() {    // fake an onEnter()
  257.   if (autoenter)
  258.     PostMessage(editWnd, WM_CHAR, VK_RETURN, 0);
  259.   return 0;
  260. }
  261.  
  262. void EditWnd::setAutoEnter(int a) {
  263.   autoenter = a;
  264. }
  265.  
  266. int EditWnd::getTextLength() { // TOTALLY NONPORTABLE AND TOTALLY DIRTY
  267.  
  268.   HFONT font = (HFONT)SendMessage(editWnd, WM_GETFONT, 0, 0);
  269.   
  270.   HDC sdc = GetDC(NULL);
  271.   HDC dc = CreateCompatibleDC(sdc);
  272.   ReleaseDC(NULL, sdc);
  273.  
  274.   HFONT oldfont = (HFONT)SelectObject(dc, font);
  275.   
  276.   SIZE s;
  277.   GetTextExtentPoint32(dc, outbuf, STRLEN(outbuf), &s);
  278.   SelectObject(dc, oldfont);
  279.   DeleteDC(dc);
  280.   return s.cx + SendMessage(editWnd, EM_GETMARGINS, 0, 0)*2+2;
  281. }
  282.  
  283. HWND EditWnd::getEditWnd() {
  284.   return editWnd;
  285. }
  286.